package com.woniuxy.stall.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.certified.dao.CertifiedInfo;
import com.woniuxy.core.exception.stallException.StallException;
import com.woniuxy.core.exception.stallException.StallExceptionCode;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.staticCode.StallInfoCode;
import com.woniuxy.core.util.DateUtil;
import com.woniuxy.renters.dao.RentersInfo;
import com.woniuxy.stall.dao.StallInfo;
import com.woniuxy.stall.dao.StallRentTime;
import com.woniuxy.stall.dto.CertifiedAccount;
import com.woniuxy.stall.feign.CertifiedClient;
import com.woniuxy.stall.feign.RentersClient;
import com.woniuxy.stall.mapper.StallInfoMapper;
import com.woniuxy.stall.mapper.StallRentTimeMapper;
import com.woniuxy.stall.service.StallInfoService;
import com.woniuxy.stall.service.StallRentTimeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.stall.service.param.CerUpStallEveryDayParam;
import com.woniuxy.stall.service.param.RenterUpStallEveryDayParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 凌玉春
 * @since 2024年01月15日
 */
@Service
public class StallRentTimeServiceImpl extends ServiceImpl<StallRentTimeMapper, StallRentTime> implements StallRentTimeService {

    @Autowired
    private RentersClient rentersClient;
    @Autowired
    private StallRentTimeMapper stallRentTimeMapper;
    @Autowired
    private StallInfoService stallInfoService;
    @Autowired
    private CertifiedClient certifiedClient;
    @Override
    public void upStallEveryDay(RenterUpStallEveryDayParam param) {
        Integer stallInfoId = param.getStallInfoId();
        Integer renterInfoId = param.getRenterInfoId();
        Result<RentersInfo> rentersInfoResult = rentersClient.getRentersInfoById(renterInfoId);
        RentersInfo rentersInfo = rentersInfoResult.getData();
        String stringIds = rentersInfo.getStallInfoId();
        JSONArray jsonArray = JSONUtil.parseArray(stringIds);
        List<Integer> integerList = jsonArray.toList(Integer.class);
        boolean b = integerList.contains(stallInfoId);
        if (!b){
            throw new StallException(StallExceptionCode.STALL_NOT_EXIST);
        }

        StallInfo stallInfo = stallInfoService.getById(stallInfoId);
        System.out.println("oooooooooooooooooo");
        System.out.println(stallInfo.getStallUpDownState());
        Integer stallUpDownState = stallInfo.getStallUpDownState();
//        if (stallUpDownState == StallInfoCode.STALL_UP.getCode()){
//            throw new StallException(StallExceptionCode.STALL_UPING);
//        }
        String startTime = param.getStartTime();
        String endTime = param.getEndTime();
        StallRentTime stallRentTime = BeanUtil.toBean(stallInfo, StallRentTime.class);
//        LocalTime localStartTime = DateUtil.StringToLocalTime(startTime);
//        LocalTime localEndTimeTime = DateUtil.StringToLocalTime(endTime);
//        LocalTime startTime = LocalTime.parse(param.getStartTime());
//        LocalTime endTime = LocalTime.parse(param.getEndTime());
        stallRentTime.setRentStartTime(startTime);
        stallRentTime.setRentEndTime(endTime);
        Integer stallInfoAffiliation = stallInfo.getStallInfoAffiliation();
        if (stallInfoAffiliation==StallInfoCode.STALL_OF_CERTIFIED.getCode()){
            //物业自有车位
            stallRentTime.setStallOwnerTyep(StallInfoCode.STALL_OF_CERTIFIED.getCode());
            Integer certifiedInfoId = stallInfo.getCertifiedInfoId();
            Result<CertifiedInfo> certifiedInfoResult = certifiedClient.findCertifiedInfoById(certifiedInfoId);
            CertifiedInfo certifiedInfo = certifiedInfoResult.getData();
            String certifiedInfoName = certifiedInfo.getCertifiedInfoName();
            stallRentTime.setStallOwner(certifiedInfoName);
        }else {
            //业主车位
            stallRentTime.setStallOwnerTyep(StallInfoCode.STALL_OF_RENTER.getCode());
            String rentersInfoName = rentersInfo.getRentersInfoName();
            stallRentTime.setStallOwner(rentersInfoName);
        }
        stallRentTimeMapper.insert(stallRentTime);
    }

    @Override
    public void cerUpStallEveryDay(CerUpStallEveryDayParam param) {
        Set<Integer> stallInfoIds = param.getStallInfoIds();
        List<StallInfo> stallInfos=stallInfoService.getStallInfosByIds(stallInfoIds);
        for (StallInfo stallInfo : stallInfos) {
            if (stallInfo.getCertifiedInfoId()!=param.getCertifiedId()){
                throw new StallException(StallExceptionCode.STALL_NONE_OR_CHECKED);
            }
        }
        List<StallRentTime> list=new ArrayList<>();
        for (StallInfo stallInfo : stallInfos) {
            StallRentTime stallRentTime = BeanUtil.toBean(stallInfo, StallRentTime.class);
            stallRentTime.setStallOwnerTyep(StallInfoCode.STALL_OF_CERTIFIED.getCode());
            Integer certifiedInfoId = stallInfo.getCertifiedInfoId();
            Result<CertifiedInfo> certifiedInfoResult = certifiedClient.findCertifiedInfoById(certifiedInfoId);
            CertifiedInfo certifiedInfo = certifiedInfoResult.getData();
            String certifiedInfoName = certifiedInfo.getCertifiedInfoName();
            stallRentTime.setStallOwner(certifiedInfoName);
            String startTime = param.getStartTime();
            String endTime = param.getEndTime();
//            LocalTime localStartTime = DateUtil.StringToLocalTime(startTime);
//            LocalTime localEndTimeTime = DateUtil.StringToLocalTime(endTime);
//            LocalTime startTime = LocalTime.parse(param.getStartTime());
//            LocalTime endTime = LocalTime.parse(param.getEndTime());
            stallRentTime.setRentStartTime(startTime);
            stallRentTime.setRentEndTime(endTime);
            list.add(stallRentTime);
        }
        saveBatch(list);
    }

    @Override
    public void delTabByStallId(Integer stallInfoId) {
        QueryWrapper<StallRentTime> wrapper = new QueryWrapper<>();
        wrapper.eq("stall_info_id",stallInfoId);
        stallRentTimeMapper.delete(wrapper);
    }

    @Override
    public StallRentTime getStallRentTimeByStallId(Integer stallInfoId) {
        QueryWrapper<StallRentTime> wrapper = new QueryWrapper<>();
        wrapper.eq("stall_info_id",stallInfoId);
        StallRentTime stallRentTime = stallRentTimeMapper.selectOne(wrapper);
        if (stallRentTime==null){
            throw new StallException(StallExceptionCode.STALL_RENT_TIME_NOT_HAVE);
        }
        return stallRentTime;
    }
}
